package com.lijinzhe.engine;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.dodos2d.layers.DDScene;
import org.dodos2d.nodes.DDDirector;
import org.dodos2d.nodes.DDSprite;
import org.dodos2d.opengl.DDGLSurfaceView;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;

public class GameEngineActivity extends Activity {
	/** Called when the activity is first created. */
	public static ArrayList<Bitmap>	list	= new ArrayList<Bitmap>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		DDGLSurfaceView mGLSurfaceView = new DDGLSurfaceView(this);

		setContentView(mGLSurfaceView);
		DDDirector.sharedDirector().attachInView(mGLSurfaceView);
		DDDirector.sharedDirector().setLandscape(true);

		DDScene scene = DDScene.node();

		DDSprite sprite = DDSprite.createSprite("powered.png");
		scene.addChild(sprite);

		DDDirector.sharedDirector().pushScene(scene);
		try {
			for (int i = 0; i < 100; i++) {
				int before = getMemoryInfo();
				Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("seafloor_bg.jpg"));
				list.add(bitmap);
				Log.d("lijinzhe", "Memory:  " + i + "  " + (getMemoryInfo() - before) + "K");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//	private String getMemoryInfo() {
	//		ProcessBuilder cmd;
	//		String result = new String();
	//
	//		try {
	//			String[] args = { "/system/bin/cat", "/proc/meminfo" };
	//			cmd = new ProcessBuilder(args);
	//
	//			Process process = cmd.start();
	//			InputStream in = process.getInputStream();
	//			byte[] re = new byte[1024];
	//			while (in.read(re) != -1) {
	//				System.out.println(new String(re));
	//				result = result + new String(re);
	//
	//			}
	//			in.close();
	//		} catch (IOException ex) {
	//			ex.printStackTrace();
	//		}
	//		return result;
	//
	//	}

	public int getMemoryInfo() {
		ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
		am.getMemoryInfo(outInfo);
		return (int) (outInfo.availMem / 1024);
		//		Log.d("lijinzhe", "Memory:  " + s + "  " + outInfo.availMem + "Byte    " + outInfo.availMem / 1024 + "K");
	}

}
